package com.eapt.erp.eds.out.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.eapt.base.EAPTBaseAction;
import com.eapt.constants.EabaCEConstants;
import com.eapt.erp.eds.out.form.EdsLogisSearchCondition;
import com.eapt.erp.eds.out.form.EdsLogisSearchForm;
import com.eapt.query.EabaCEQuery;
import com.eapt.query.EdsLogisQuery;
import com.eapt.vo.EabaC;
import com.eapt.vo.EabaCE;
import com.eapt.vo.EdsLogis;
import com.stony.core.util.PageRange;

public class EdsLogisSearchAction extends EAPTBaseAction {

	@Override
	protected ActionForward doWork(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		EdsLogisSearchForm searchForm = (EdsLogisSearchForm) form;
		searchForm.initConditionPost(request);
		EdsLogisSearchCondition condition = (EdsLogisSearchCondition) searchForm.getCondition();
		
		ActionMessages errors = validate(condition);
		if (errors.size() > 0) {
			this.saveErrors(request, errors);
			return mapping.getInputForward();
		}
		
		List<EdsLogis> result = new ArrayList<EdsLogis>();
		List<Long> bind = getBindCustomer(request);
		if (bind.size() > 0) {
			EdsLogisQuery query = new EdsLogisQuery();
			if (StringUtils.isNotBlank(condition.getOutshiptono())) {
				query.setOutshiptono(Long.valueOf(condition.getOutshiptono().toUpperCase().replace("EASD", "")));
			}
			
			if (StringUtils.isNotBlank(condition.getDate1())) {
				query.setDate(condition.getDate1(), condition.getDate2());
			}
			
			query.setShipto(bind);
		
			PageRange range = searchForm.getPageinfo();
			range.setPageSize(MAX_RESULTS);
			query.setPageRange(range);
			query.execute();
			result = query.getResults();
		}

		request.setAttribute("result", result);
		return mapping.findForward("success");
	}
	
	private ActionMessages validate(EdsLogisSearchCondition condition) {
		ActionMessages errors = new ActionMessages();
		String outshiptono = condition.getOutshiptono();
		if (StringUtils.isNotBlank(outshiptono)) {
			try {
				Long.valueOf(condition.getOutshiptono().toUpperCase().replace("EASD", ""));
			} catch (Exception e) {
				errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("sql.grammar.error", ""));
			}
		}
		
		return errors;
	}

	private List<Long> getBindCustomer(HttpServletRequest request) {
		List<Long> ntcnos = new ArrayList<Long>();
		EabaCEQuery ceQuery = new EabaCEQuery();
		ceQuery.setEmpno(getEmployee(request).getEmpno());
		ceQuery.setCetype(EabaCEConstants.CETYPE_DSBD);
		ceQuery.fetchCno();
		ceQuery.execute();
		List<EabaCE> ceList = ceQuery.getResults();
		for (EabaCE ce : ceList) {
			EabaC c = ce.getCno();
			if (null != c) {
				ntcnos.add(c.getCno());
			}
		}
		return ntcnos;
	}

}
